﻿using MVTVV06;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for MVTVV06.xaml
    /// </summary>
    public partial class MVTVV06 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        MVTVV06Data m_Data, m_DataX;
        DataSet SetData = new DataSet();
        public SETVV setUI
        {
            get
            {
                return (SETVV)this.TryFindResource("SETUI");
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        MVTVV06Report m_rpt;
        DataTable tableUI;
        private DataRow dwUI;
        ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public MVTVV06()
        {
            InitializeComponent();
            this.Name = "发货单";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new MVTVV06Data(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("VKPAUFKRS", typeof(string));
            tableUI.Columns.Add("VKPAUFLFD", typeof(string));
            //txtVKPAUFKRS.Focus();
            return;
            SetUpFtp();
            btnLogFTP_Click(null, null);
            /*
            txtVKPAUFKRS.Text = "Z ";
            txtVKPAUFLFD.Text = "002030";
             * */

        }
        public void btnLogFTP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ftpsite = txtFTPSite.Text.Trim();
                string ftpport = txtFTPPort.Text.Trim();
                string ftpuser = txtFTPUser.Text.Trim();
                CheckFtp(ftpsite, ftpport, ftpuser, txtFTPPwd, IsBelongs2Admin.IsChecked == true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("FTP登录信息有误。请联系你们的破管理员。");
            }
            finally
            {
                ftpClient.Close();
            }
        }
        private void ReloadVVTLIEF(DataRow dwVVLIEFSCH)
        {
            if (setUI.Tables.Contains("VKPOS"))
            {
                setUI.Tables["VKPOS"].Clear();
                setUI.AcceptChanges();
            }
            if (setUI.Tables.Contains("WBBST"))
            {
                setUI.Tables["WBBST"].Clear();
                setUI.AcceptChanges();
            }
            if(SetData.Tables.Contains("VKPOS"))
            {
                SetData.Tables["VKPOS"].Clear();
                SetData.AcceptChanges();
            }
            if (SetData.Tables.Contains("WBBST"))
            {
                SetData.Tables["WBBST"].Clear();
                SetData.AcceptChanges();
            }
            DataSet setResult = new DataSet();
            setResult = m_Data.LoadVKPOS(dwVVLIEFSCH);
            setUI.Merge(setResult);
            SetData.Merge(setResult);
            setResult = m_Data.GetWBBST(dwVVLIEFSCH);
            setUI.Merge(setResult);
            SetData.Merge(setResult);
            dlgVKPOS.DataContext = setUI.Tables["VKPOS"];
            listWBBST.DataContext = setUI.Tables["WBBST"];
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                m_rpt = new MVTVV06Report(dicParamater);
                m_rpt.dsSource = SetData;
                m_rpt.PrintType = PrintType;
                m_rpt.ftpClient = ftpClient;
                if (m_Data.exAgent._app == null)
                {
                    m_Data.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt.proxy = this.proxy;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.exAgent._app.Visible = true;
                m_rpt.WriteExcelReport("EXCEL");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }

        private void btnGetVVLIEF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                /*string vvliefsch = m_Data.Right("00000000" + this.txtVVLIEFSCH.Text, 8);
                if (vvliefsch != "00000000")
                {
                    LoadData(vvliefsch);
                    return;
                }*/
                setUI.Clear();
                /*string krs = m_Data.Left(txtVKPAUFKRS.Text.ToString().Trim() + "  ", 2);
                string lfd = m_Data.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);
                setUI.Merge(m_Data.LoadVKPOS(krs, lfd));
                */
                dwUI = tableUI.NewRow();
                //dwVKPOS = setUI.VKPOS.NewVKPOSRow();
                dwUI["VKPAUFKRS"] = m_Data.Left(txtVKPAUFKRS.Text.ToString().Trim() + "  ", 2);
                dwUI["VKPAUFLFD"] = m_Data.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);

                LoadData(dwUI);
                //ReloadVVTLIEF(dwVKPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgVVLIEF_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVVLIEFSCH = drv.Row;
                this.txtVVLIEFSCH.Text = dwVVLIEFSCH["VVLIEFSCH"].ToString();
                ReloadVVTLIEF(dwVVLIEFSCH);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        /*private void txtVVLIEFSCH_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                return;
                string vvliefsch = m_Data.Right("00000000" + ((TextBox)sender).Text, 8);
                if (vvliefsch == "00000000")
                    return;
                LoadData(vvliefsch);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
           
        }*/
        private void LoadData(DataRow dwVKPOS)
        {
            string tbname = "";
            try
            {
                setUI.Clear();
                setUI.AcceptChanges();
                SetData = new DataSet();
                DataSet result = m_Data.BuildData(dwVKPOS);
                SetData = result;
                foreach (DataTable tb in result.Tables)
                {
                    try
                    {
                        tbname = tb.TableName;
                        setUI.Merge(tb,false,MissingSchemaAction.Ignore);
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex.StackTrace, ex);
                    }
                }
                dlgVKKOPF.DataContext = setUI.Tables["VKKOPF"];
                dlgVVLIEF.DataContext = setUI.Tables["VVLIEF"];
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void txtVKPAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnGetVVLIEF_Click(null, null);
        }
    }
}
